/**
 * Copyright (c) 2009-2011, chunquedong(YangJiandong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * History:
 *     2010-05-05  Jed Young  Creation
 */
package chunmap.data.index;

import java.util.ArrayList;
import java.util.List;

import chunmap.data.feature.Feature;
import chunmap.data.feature.FeatureSet;
import chunmap.data.feature.Shape;
import chunmap.data.feature.VisitAction;
import chunmap.data.feature.VisitFunc;
import chunmap.model.elem.Envelope;

public class FeatureList extends FeatureSet{
	private List<Feature> _features;

    public FeatureList()
    {
        _features = new ArrayList<Feature>();
    }

    //------------------------------------------------------------------------
    @Override
    public void insert(Feature f)
    {
        _features.add(f);
    }
    
    @Override
    public boolean remove(Feature f)
    {
        return _features.remove(f);
    }
    
    @Override
    public void search(Envelope envelope, VisitFunc fun)
    {
        for (Feature f : _features)
        {
            if (envelope.hasIntersect(f.getEnvelop()))
            {
                if (!fun.execute(f)) break;
            }
        }
    }
    
    @Override
    public void select(Envelope envelope, VisitAction fun)
    {
        for (Feature f : _features)
        {
            if (!envelope.hasIntersect(f.getEnvelop())) continue;

            if (f instanceof Shape)
            {
                Shape shape = (Shape)f;
                if (envelope.hasIntersect(shape.getEnvelop()))
                {
                    fun.execute(f);
                }
            }
            else
            {
                fun.execute(f);
            }
        }
    }
    
    @Override
    public void find(VisitFunc fun)
    {
    	for (Feature f : _features)
        {
            if (!fun.execute(f)) break;
        }
    }
    
    @Override
    public void each(VisitAction fun)
    {
    	for (Feature f : _features)
        {
            fun.execute(f);
        }
    }

}